package com.zjl.litemall.admin.controller;

import com.zjl.litemall.core.util.ResponseUtil;
import com.zjl.litemall.core.validator.Sort;
import com.zjl.litemall.db.domain.LitemallUser;
import com.zjl.litemall.db.service.LitemallUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/admin/user")
@Api(tags = "用户管理模块")
public class AdminUserController {
    //
    private final Log logger = LogFactory.getLog(AdminUserController.class);

    @Autowired
    private LitemallUserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "会员管理--查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "分页",   dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "分页", dataType = "Integer"),
            @ApiImplicitParam(name = "sort", value = "分类", dataType = "String"),
            @ApiImplicitParam(name = "order", value = "排序", dataType = "String")
    })
    public Object list(String username, String mobile,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Sort @RequestParam(defaultValue = "desc")String order) {
        List<LitemallUser> userList = userService.querySelective(username, mobile, page, limit, sort, order);
        return ResponseUtil.okList(userList);
    }

    @PostMapping("/update")
    @ApiOperation(value = "会员管理--编辑")
    @ApiImplicitParam(value = "User实体")
    public Object userUpdate(@RequestBody LitemallUser user){
        return ResponseUtil.ok(userService.updateById(user));
    }

    @GetMapping("/detail")
    @ApiOperation(value = "会员管理--详情")
    @ApiImplicitParam(name = "id", value = "用户id")
    public Object userDetails(@NotNull Integer id){
        LitemallUser litemallUser = userService.selectById(id);
        return ResponseUtil.ok(litemallUser);
    }

}
